﻿@model IEnumerable<SAKS.Models.User>

@{
    ViewBag.Title = "人员管理";
    Layout = "_ProjectLayout.cshtml";
}
<link href="@Url.Content("~/Content/themes/base/validationEngine.jquery.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.validationEngine.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validationEngine-ch.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.prompt.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.mytabs.js")" type="text/javascript"></script>
<script type="text/javascript">

    $(function () {
        var projectid = $("#projectid").get(0).value;
        var admin = $("#admin").get(0).value;
        var isadmin = $("#isadmin").get(0).value;

        var username;

        $("#tabs").mytabs({
            show: function (panelid) {
                if (panelid == "#tabs-1") {
                    loadJoinRequets();
                } else if (panelid == "#tabs-0") {
                    loadMembers();
                }
            }
        });
        //        $("#tabs").tabs({
        //            show: function (event, ui) {
        //                if (ui.panel.id == "tabs-1") {
        //                    loadJoinRequets();
        //                } else if (ui.panel.id == "tabs-0") {
        //                    loadMembers();
        //                }
        //            }
        //        });

        function loadMembers() {
            var members = $("#members").text("加载中...");
            $.get(
                "/Project/Member",
                { "projectid": projectid, "p304": new Date().getMilliseconds() },
                function (data) {
                    if (data.State == "Succeed") {
                        members.text("");
                        members.append("<ul style='list-style:none'></ul>");
                        var length = data.Data.length;
                        if (length > 1) {
                            var ul = $("ul", members);
                            for (var i = 0; i < length; i++) {
                                if (data.Data[i] != admin)
                                    ul.append("<li> <input type='radio' name='username' id='" + data.Data[i] + "'/><a href='#' class='username' >" + data.Data[i] + "</a></li>");
                            }
                            if (isadmin == "True") {
                                members.append("<button id='appointAdmin' >任命为管理员</button><button id='remove'>移除</button><span id='prompt'></span>");
                                $("button#appointAdmin", members).button().click(function () {
                                    var selected = $("input:checked", members)
                                    if (selected.length != 1) $("#prompt").prompt('alert', '请选中一个成员');
                                    else {
                                        username = selected.attr("id");
                                        $("#dlgAppointPrompt").text("您确定要任命用户  ‘" + username + "’ 为新的管理员吗？任命后您将失去管理员的身份。").dialog("open");
                                    }
                                });
                                $("button#remove", members).button().click(function () {
                                    var selected = $("input:checked", members)
                                    if (selected.length != 1) $("#prompt").prompt('alert', '请选中一个成员');
                                    else {
                                        username = selected.attr("id");
                                        $("#dlgRemovePrompt").text("您确定要移除用户‘" + username + "’吗？").dialog("open");
                                    }
                                });
                                $("#members input[type='radio']").click(function () {
                                    $.prompt('close');
                                });
                            }
                        } else {
                            members.text("项目中无其他成员");
                        }
                    } else {
                        members.text(data.Message);
                    }
                }
            );
        }
        function loadJoinRequets() {
            $("#tabs-1").text("加载中...");
            $.post(
                "/Project/JoinRequests",
                { "Id": projectid },
                function (data) {
                    var length = data.length;
                    if (length == 0) {
                        $("#tabs-1").text("没有用户请求加入本项目！");
                    } else {
                        $("#tabs-1").text("");
                        $("#tabs-1").append("<table><tr><th></th><th>用户</th><th>备注</th></tr></table>");
                        var t = $("#tabs-1 table");
                        for (var i = 0; i < length; i++) {
                            t.append("<tr requestId ='" + data[i].Id + "' username='" + data[i].UserName + "'><td><a class='approve' href='#'>同意</a>&nbsp<a class='reject' href='#'>拒绝</a></td><td><a  href='#' class='username'>" + data[i].UserName + "</a></td><td>" + data[i].Message + "</td></tr>");
                        }

                        $("a.approve", t).click(function () {
                            var parent = $(this).parent().parent();
                            var username = parent.attr("username");
                            var requestid = parent.attr("requestId");
                            $.post(
                                "/Project/RequestApprove",
                                { "username": username, "projectid": projectid, "requestid": requestid },
                                function (data) {
                                    if (data.State == "Succeed") {
                                        parent.fadeOut(500).remove();
                                    }
                                    showMessage(data.Message);
                                }
                            );

                        });
                        $("a.reject", t).click(function () {
                            var parent = $(this).parent().parent();
                            var username = parent.attr("username");
                            var requestid = parent.attr("requestId");
                            $.post(
                                "/Project/RequestReject",
                                { "username": username, "projectid": projectid, "requestid": requestid },
                                function (data) {
                                    if (data.State == "Succeed") {
                                        parent.fadeOut(500).remove();
                                    }
                                    showMessage(data.Message);
                                }
                            );

                        });
                    }

                }
            );
        }
        $("#dlgAppointPrompt").dialog({
            closeOnEscape: true,
            title: "重新任命管理员",
            autoOpen: false,
            modal: true,
            buttons: [
                 {
                     text: "确定",
                     click: function () {
                         startProcess();
                         $.post(
                             "/Project/AppointAdmin",
                             { "username": username, "projectid": projectid },
                             function (data) {
                                 if (data.State == "Succeed") {
                                     $("#adminname").text(username);
                                     $("#appointAdmin,#remove").hide();
                                     $("li a[href='#tabs-1']").parent().hide();
                                     $("#tabs-1").hide();
                                     $("input#" + username).next(".username").text(admin);
                                     admin = username;
                                 }
                                 endProcess();
                                 showMessage(data.Message);
                             }
                         );
                         $("#dlgAppointPrompt").dialog("close");
                     }
                 },
                {
                    text: "关闭",
                    click: function () { $("#dlgAppointPrompt").dialog("close"); }
                }
            ]
        });
        $("#dlgRemovePrompt").dialog({
            closeOnEscape: true,
            title: "移除用户",
            autoOpen: false,
            modal: true,
            buttons: [
                 {
                     text: "确定",
                     click: function () {
                         startProcess();
                         $.post(
                             "/Project/RemoveUser",
                             { "username": username, "projectid": projectid },
                             function (data) {
                                 if (data.State == "Succeed") {
                                     $("#members input#" + username).parent().fadeOut('slow').remove();
                                     if ($("div#members li").length == 0) {
                                         $("#appointAdmin,#remove").hide();
                                         $("div#members").text("项目中无其他成员");
                                     }
                                 }
                                 endProcess();
                                 showMessage(data.Message);
                             }
                         );
                         $("#dlgRemovePrompt").dialog("close");
                     }
                 },
                {
                    text: "关闭",
                    click: function () { $("#dlgRemovePrompt").dialog("close"); }
                }
            ]
        });
        function startProcess() { }
        function endProcess() { }
        function showMessage(message) { }
        
    });

</script>
<div id="dlgRemovePrompt"></div>
<div id="dlgAppointPrompt"></div>
<input type="hidden" id="projectid" value="@ViewBag.ProjectId" />
<input type="hidden" id="admin" value="@ViewBag.Admin" />
<input type="hidden" id="isadmin" value="@ViewBag.IsAdmin" />
<div id="tabs">
    <ul>
        <li><a href="#tabs-0">项目人员</a></li>
        @if (ViewBag.IsAdmin == true)
        { 
        <li><a href="#tabs-1">加入请求</a></li>
        }
    </ul>

    <div id="tabs-0">
        <p>管理员：<a href="#" id="adminname" class="username">@ViewBag.Admin</a></p>
        <p>成员：</p>
        <div id="members"></div>
        
    </div>

@if (ViewBag.IsAdmin == true)
{ 
    <div id="tabs-1">

    </div>
}
</div>


